var checkNameMsg = $('#checkName'),
    checkPassword = $('#passwordCheck'),
    checkFirstName = $('#firstNameCheck'),
    checkLastName = $('#lastNameCheck'),
    checkEmail = $('#emailCheck'),
    checkPhone = $('#phoneCheck'),
    checkAddress1 = $('#address1Check'),
    checkAddress2 = $('#address2Check'),
    checkCity = $('#cityCheck'),
    checkState = $('#stateCheck'),
    checkZip = $('#zipCheck'),
    checkCountry = $('#countryCheck');

$(function (){
    $('#username').on('blur',function (){
        var username = $(this).val(),
           validateMsg = checkAccount(username);
        if (validateMsg === ''){
            $.ajax({
                type       : 'POST',
                url        : 'http://localhost:8080/_war_exploded/register?username='+username,
                success    : function (data){
                    checkNameMsg.text(data);
                },
                error      : function (errorMsg){
                    console.log(errorMsg);
                }
            })
        }else {
            checkNameMsg.text(validateMsg);
        }
    });

    $('#repeatedPassword').on('blur',function (){
       var password = $('#password').val(),
         repeatedPassword = $(this).val();
       if (password !== repeatedPassword){
           checkPassword.text('两次密码不一致');
       }else {
           checkPassword.text('');
       }
    });

    $('#firstName').on('blur',function (){
        if ($(this).val() === ''){
            checkFirstName.text('FirstName不能为空');
        }else {
            checkFirstName.text('');
        }
    });
    $('#lastName').on('blur',function (){
        if ($(this).val() === ''){
            checkLastName.text('LastName不能为空');
        }else {
            checkLastName.text('');
        }
    });
    $('#email').on('blur',function (){
        if ($(this).val() === ''){
            checkEmail.text('email不能为空');
        }else {
            checkEmail.text('');
        }
    });
    $('#phone').on('blur',function (){
        if ($(this).val() === ''){
            checkPhone.text('phone不能为空');
        }else {
            checkPhone.text('');
        }
    });
    $('#address1').on('blur',function (){
        if ($(this).val() === ''){
            checkAddress1.text('address1不能为空');
        }else {
            checkAddress1.text('');
        }
    });
    $('#address2').on('blur',function (){
        if ($(this).val() === ''){
            checkAddress2.text('address2不能为空');
        }else {
            checkAddress2.text('');
        }
    });
    $('#city').on('blur',function (){
        if ($(this).val() === ''){
            checkCity.text('city不能为空');
        }else {
            checkCity.text('');
        }
    });
    $('#state').on('blur',function (){
        if ($(this).val() === ''){
            checkState.text('state不能为空');
        }else {
            checkState.text('');
        }
    });
    $('#zip').on('blur',function (){
        if ($(this).val() === ''){
            checkZip.text('zip不能为空');
        }else {
            checkZip.text('');
        }
    });
    $('#country').on('blur',function (){
        if ($(this).val() === ''){
            checkCountry.text('country不能为空');
        }else {
            checkCountry.text('');
        }
    });
});

function checkAccount(username){
    if (username === ''||username === null||username.length === 0){
        return '用户名不能为空';
    }
    if (username.length<5){
        return '用户名长度不能小于5';
    }
    return '';
}

